package main

import "fmt"

//TreeNode
type TreeNode struct {
	Value       int;
	left, right *TreeNode;
}

//中序遍历
func (node *TreeNode) TranvserseFunc(f func(*TreeNode)) {
	if node == nil {
		return
	}
	node.left.TranvserseFunc(f)
	f(node)
	node.right.TranvserseFunc(f)
}

//中序遍历管道
func (node *TreeNode) TranvserseWithChannel() chan *TreeNode {
	ch := make(chan *TreeNode)
	go func() {
		node.TranvserseFunc(func(node *TreeNode) {
			ch <- node
		})
		close(ch)
	}()
	return ch
}

func main() {

	node := TreeNode{1, &TreeNode{2, &TreeNode{500, nil, nil}, nil}, &TreeNode{3, &TreeNode{4, nil, nil}, nil}}
	channel := node.TranvserseWithChannel()
	max := 0
	for i := range channel {
		if i.Value > max {
			max = i.Value
		}
	}
	fmt.Printf("max node value is %d\n", max)
}
